﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tilver.Data;
using Tilver.Common;
using Weather.DAL.CoWeather;

namespace Weather.BLL.Weather
{
    public class FcstTextLocalBO
    {
        /// <summary>
        /// 获取某个站下的天气预报信息
        /// </summary>
        /// <param name="stationNum"></param>
        /// <returns></returns>
        public List<FcstTextLocalEO> GetFcstTextLocalByStationNum(string stationNum)
        {
            /*
             SELECT [fcst_type],[fcst_title],[fcst_text],[renewTime] FROM [Fcst_Text_Local] 
where stationnum = '58553' and id in (select MAX(id) from [Fcst_Text_Local] where stationnum = '58553' group by fcst_type) 
order by fcst_type asc
             * 0 未来3小时
             * 1 短期预报
             * 2 一周预报
             * 3 
             * 4 生活提示
             * 5 
             * 6 历史上的今天
             * 7 昨日天气
             * 8 
             * 9 预警信号
             */
            var list = new List<FcstTextLocalEO>();
            var para = new List<DbParameter>();
            var factory = DbDataFactory.Create("COWeather");
            var sql = new StringBuilder();
            sql.Append(" SELECT [fcst_type],[fcst_title],[fcst_text],[renewTime] FROM [Fcst_Text_Local] where stationnum = @stationnum and id in ");
            sql.Append("(select MAX(id) from [Fcst_Text_Local] where stationnum = @stationnum group by fcst_type) order by fcst_type asc ");
            {
                var p = new SqlParameter("@stationnum", SqlDbType.VarChar);
                p.Value = stationNum;
                para.Add(p);
            }

            var sdr = factory.ExecuteReader(sql.ToString(), para);
            while (sdr.Read())
            {
                list.Add(new FcstTextLocalEO()
                {
                    FcstType = sdr["fcst_type"] is DBNull ? 0 : sdr["fcst_type"].ToString().ToInt32(0),
                    FcstTitle = sdr["fcst_title"] is DBNull ? "" : sdr["fcst_title"].ToString(),
                    FcstText = sdr["fcst_text"] is DBNull ? "" : sdr["fcst_text"].ToString(),
                    RenewTime = sdr["renewTime"] is DBNull ? DateTime.Now : sdr["renewTime"].ToString().ToDateTime()
                });
            }
            sdr.Close();
            return list;
        }
    }
}
